<!--  -->
<script setup lang="ts">
import { computed } from 'vue'
import { isExternal as external } from '@/utils/validate'

interface Props {
  icon: string
  className?: string
}

const props = withDefaults(defineProps<Props>(), {
  className: ''
})

/**
 * 判断当前图标是否为外部图标
 */
const isExternal = computed<boolean>(() => external(props.icon))
/**
 * 外部图标样式
 */
const styleExternalIcon = computed<any>(() => {
  return {
    mask: `url(${props.icon}) no-repeat 50% 50%`,
    '-webkit-mask': `url(${props.icon}) no-repeat 50% 50%`
  }
})
/**
 * 内部图标
 */
const iconName = computed<string>(() => `#icon-${props.icon}`)
</script>

<template>
  <!-- 外部图标 -->
  <div v-if="isExternal" :style="styleExternalIcon" :class="className" class="svg-external-icon svg-icon"></div>
  <!-- 内部图标 -->
  <svg v-else class="svg-icon" :class="className" aria-hidden="true">
    <use :xlink:href="iconName" />
  </svg>
</template>

<style lang="scss" scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor !important;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
